const { ethers } = require("hardhat")

async function main() {
    const fundMeFactory = await ethers.getContractFactory("FundMe")
    console.log("contract deploying....")
    const fundMe = await fundMeFactory.deploy()
    await fundMe.waitForDeployment()
    console.log(
        `contract has been deployed successfully, contract address is ${fundMe.target}`
    )
    const [firstAccount, secondAccount] = await ethers.getSigners()
    const fundTx = await fundMe.fund({ value: ethers.parseEther("0.5") })
    await fundTx.wait()
    console.log(
        `两个账户分别是 ${firstAccount.address} and ${secondAccount.address}`
    )
    const balanceOfContract = await ethers.provider.getBalance(fundMe.target)
    console.log(`合约的余额是 ${balanceOfContract}`)

    const fundTxWithSecondAccount = await fundMe
        .connect(secondAccount)
        .fund({ value: ethers.parseEther("0.5") })
    await fundTxWithSecondAccount.wait()

    // check balance of contract
    const balanceOfContractAfterSecondFund = await ethers.provider.getBalance(
        fundMe.target
    )
    console.log(`合约的总金额 is ${balanceOfContractAfterSecondFund}`)

    // check mapping
    const firstAccountbalanceInFundMe = await fundMe.fundersToAmount(
        firstAccount.address
    )
    const secondAccountbalanceInFundMe = await fundMe.fundersToAmount(
        secondAccount.address
    )
    console.log(
        `Balance of first account ${firstAccount.address} is ${firstAccountbalanceInFundMe}`
    )
    console.log(
        `Balance of second account ${secondAccount.address} is ${secondAccountbalanceInFundMe}`
    )
}

main()
    .then()
    .catch((error) => {
        console.error(error)
        process.exit(0)
    })
